package com.powell.health.dao.sqlhelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Pair;
import com.powell.health.activity.R;
import com.powell.health.dao.table.CategoryTable;
import com.powell.health.dao.table.DetailTable;
import com.powell.health.dao.table.ItemTable;

/**
 * Created with IntelliJ IDEA.
 * User: tpowell
 * Date: 1/5/13
 * Time: 8:44 PM
 * To change this template use File | Settings | File Templates.
 */
public class SqlHealthManagerDatabaseHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "HealthManager.db";
    private static String[] categoriesArray;

    public SqlHealthManagerDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        loadStaticResources(context);
    }

    private void loadStaticResources(Context context) {
        categoriesArray = context.getResources().getStringArray(R.array.categoriesArray);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createTables(db);
        createCategories(db);
    }

    private void createCategories(SQLiteDatabase db) {
        ContentValues values = new ContentValues();
        for(String categoryName : categoriesArray){
            values.put(CategoryTable.CATEGORY_NAME, categoryName);
            db.insert(CategoryTable.TABLE_NAME, CategoryTable.CATEGORY_NAME, values);
        }
    }

    private void createTables(SQLiteDatabase db) {
        db.execSQL(CategoryTable.CREATE_TABLE);
        db.execSQL(ItemTable.CREATE_TABLE);
        db.execSQL(DetailTable.CREATE_TABLE);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        System.out.println("Called on open");
        Cursor c = db.rawQuery("select * from "+CategoryTable.TABLE_NAME, null);
        if(c.moveToFirst()){
            do{
               System.out.println(c.getString(c.getColumnIndex(CategoryTable.CATEGORY_ID)));
            }while (c.moveToNext());
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println("Called on upgrade");
    }
}
